import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:intl/intl.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AddTaskPage extends StatefulWidget {
  AddTaskPage({Key key, this.title}) : super(key: key);

  final String title;
  TextEditingController controllerTaskName = new TextEditingController();
  TextEditingController controllerPeople = new TextEditingController();
  TextEditingController controllerDescription = new TextEditingController();
  TextEditingController controllerDate = new TextEditingController();

  final formKey = GlobalKey<FormState>();

  @override
  _AddTaskPageState createState() => _AddTaskPageState();
}

class _AddTaskPageState extends State<AddTaskPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Form(
      key: widget.formKey,
      child: Column(
        children: <Widget>[
          TextFormField(
            maxLines: 1,
            style: TextStyle(fontSize: 17),
            controller: widget.controllerTaskName,
            validator: (value) {
              if (value == null || value.isEmpty) {
                return '请输入事项名称';
              }
              return null;
            },
            decoration: InputDecoration(
              prefixIcon: const Icon(
                Icons.sticky_note_2,
                color: Colors.grey,
              ),
              hintText: '事项名称',
            ),
          ),
          SizedBox(height: 20),
          SizedBox(height: 20),
          dateField(widget.controllerDate),
          SizedBox(height: 20),
          TextFormField(
            maxLines: 5,
            maxLength: 100,
            style: TextStyle(fontSize: 17),
            controller: widget.controllerDescription,
            decoration: InputDecoration(
              hintText: '事项描述',
            ),
          ),
        ],
      ),
    ));
  }

  Widget dateField(TextEditingController controllerDate) {
    return Column(children: <Widget>[
      DateTimeField(
        controller: controllerDate,
        decoration: InputDecoration(
          prefixIcon: const Icon(
            Icons.access_time,
            color: Colors.grey,
          ),
          hintText: '请选择时间',
        ),
        format: DateFormat("yyyy-MM-dd HH:mm"),
        onShowPicker: (context, currentValue) async {
          final date = await showDatePicker(
              context: context,
              firstDate: DateTime(1900),
              initialDate: currentValue ?? DateTime.now(),
              lastDate: DateTime(2100));
          if (date != null) {
            final time = await showTimePicker(
              context: context,
              initialTime:
                  TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
            );
            return DateTimeField.combine(date, time);
          } else {
            return currentValue;
          }
        },
      ),
    ]);
  }
}
